$(document).ready(function(){
	
	$('input#payment_input').focus(function(){
		var amount = $(this).attr('original_fee_value');
		var number = $(this).attr('fee_number');
		$(this).keyup(function(){
			var value = $(this).val();
			$('input#amount_balance_'+number).val(numberWithCommas((+amount - +value).toFixed(2)));
			$('input#amount_balance_hidden'+number).val(+amount - +value);
		});
	});
	
	$('input#breakdown_remaining').val(numberWithCommas($('input#payment_recieved').attr('rate')));
	
	$('input.breakdown_sum').keyup(function(){
		var sum = 0;
		var remaining;
		var max = $('input#payment_recieved').attr('rate');
		$('input.breakdown_sum').each(function() {
			sum += Number($(this).val());
		remaining = +max - +sum;	
			$('input#breakdown_total').val(numberWithCommas(sum.toFixed(2)));
			$('input#breakdown_remaining').val(numberWithCommas(remaining.toFixed(2)));
			
			if(sum > max)
			{
				$('div#message').html('<div class="error">Warning total Payment Breakdown is greater than recieved Payment</div>');
			}else{
				$('div#message').html('');
			}
		});
	});
	

	try
	{
		var top = $('#follow').offset().top - parseFloat($('#follow').css('margin-top').replace(/auto/, 0));
	}catch(err){
		console.log('No Top to select');
	}
	  
	  $(window).scroll(function (event) {
		// what the y position of the scroll is
		var y = $(this).scrollTop();
	  
		// whether that's below the form
		if (y >= top) {
		  // if so, ad the fixed class
		  $('#follow').addClass('fixed');
		} else {
		  // otherwise remove it
		  $('#follow').removeClass('fixed');
		}
	  });

	function numberWithCommas(x) 
	{
		if(typeof x !== "undefined")
		{
			var parts = x.toString().split(".");
			parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			return parts.join(".");
		}
	}

});




